package com.weetop.cfw.home_page.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.ActivityUtils
import com.weetop.cfw.R
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.base.view.CloudCurriculumAndAudioSearchResultView
import com.weetop.cfw.event.VideoAndAudioSearchResultEvent
import com.weetop.cfw.home_page.fragment.AudioSearchResultFragment
import com.weetop.cfw.home_page.fragment.CloudCurriculumSearchResultFragment
import com.weetop.cfw.utils.EventBusUtils
import kotlinx.android.synthetic.main.activity_cloud_curriculum_and_audio_search_result.*


/**
 * 云课程和管理音频的搜索结果页面
 */
class CloudCurriculumAndAudioSearchResultActivity : CommonBaseActivity(),View.OnClickListener, CloudCurriculumAndAudioSearchResultView,TextView.OnEditorActionListener {

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.imageSearchResultBack -> ActivityUtils.finishActivity(this)
            R.id.imageAudioVideoSearchLogo -> beginToSearch()
        }
    }

    var keyword = ""//搜索关键字

    companion object{
        private const val SEARCH_KEY = "VIDEO_AUDIO_SEARCH_KEY"

         fun startCloudCurriculumAndAudioSearchResultActivity(context: Context,searchKey:String){
             val intent = Intent(context,CloudCurriculumAndAudioSearchResultActivity::class.java)
             intent.putExtra(SEARCH_KEY,searchKey)
             ActivityUtils.startActivity(intent)
         }
    }

    private val cloudCurriculumSearchResultFragment:CloudCurriculumSearchResultFragment = CloudCurriculumSearchResultFragment()
    private val audioSearchResultFragment:AudioSearchResultFragment = AudioSearchResultFragment()

    private val titleList = listOf("商业云课","音频管理")

    private val commonBaseFragmentList = listOf<CommonBaseFragment>(cloudCurriculumSearchResultFragment,audioSearchResultFragment)

    override fun getLayoutId() = R.layout.activity_cloud_curriculum_and_audio_search_result

    override fun initView(savedInstanceState: Bundle?) {
       setViewsOnClickListener(this,imageSearchResultBack,imageAudioVideoSearchLogo)
       editAudioVideoSearchContent.setOnEditorActionListener(this)
       initTabLayout()

       searchResultVP.adapter = SearchResultPagerAdapter(supportFragmentManager,commonBaseFragmentList,titleList)
    }

    override fun initData(savedInstanceState: Bundle?) {
          val searchKey = intent.getStringExtra(SEARCH_KEY)
          keyword = searchKey ?: ""
          editAudioVideoSearchContent.setText(keyword)
    }


    private fun initTabLayout(){
        searchResultMagicIndicator.addTab(searchResultMagicIndicator.newTab().setText("商业云课"))
        searchResultMagicIndicator.addTab(searchResultMagicIndicator.newTab().setText("音频管理"))

        searchResultMagicIndicator.setupWithViewPager(searchResultVP)
    }

    private class SearchResultPagerAdapter(fragmentManager: FragmentManager, var leaseSaleInformationFragmentList:List<CommonBaseFragment>, var titleList:List<String>):
        FragmentStatePagerAdapter(fragmentManager){

        override fun getItem(position: Int) = leaseSaleInformationFragmentList[position]

        override fun getCount() = leaseSaleInformationFragmentList.size

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
//            super.destroyItem(container, position, `object`)
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return titleList[position]
        }

    }

    /**
     *  进行搜索
     */
    override fun beginToSearch() {
        keyword = editAudioVideoSearchContent.text.toString().trim()//湖区搜索内容
        EventBusUtils.post(VideoAndAudioSearchResultEvent(searchResultMagicIndicator.selectedTabPosition,keyword))
    }


    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            beginToSearch()
            return true
        }
        return false
    }
}
